<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/19
 * Time: 8:41
 */
namespace app\admin\controller;

use app\admin\model\DepartmentModel;
use app\admin\model\MemberModel;
use app\admin\model\RoleModel;
use app\admin\model\OrderModel;
use think\Db;
use app\admin\controller\Finance;

class Statistics extends Base
{
    //调用当前用户下的部门 13 -21
    public function _initialize(){
        parent::_initialize();
        $uid = session('id');

        if (empty(session('can_see')[0])) {
            $department_id =Db::name('department')->where(['pid'=>0])->value('id');
        }else{
            $role_id = Db::name('user')->where(['id'=>$uid])->value('role_id');
            $department_id =Db::name('role')->where(['id'=>$role_id])->value('department_id');
        }
        $roleModel = new RoleModel();
        $departUserID = $roleModel->getUsersByDepartIdCansee($department_id);
        $where['id'] = array('in',$departUserID);
        //查询当前部门下的所有职员
        $departUserArr = Db::name('user')->field(['id','real_name'])->where($where)->select();
        $myArr = Db::name('department')->where('id',$department_id)->find();
        $dep_Arr = $this->DeparArr($department_id);
        $myArr['children'] = $dep_Arr;
        $departArr[0] = $myArr;
        $option = $this->arr2Tree($departArr);
        // 2018-7-16 新增(学校列表、层次列表)
        $schoolist = Db::name('school_specialty')->where(['pid'=>0])->order('id asc')->select();
        $gradelist = Db::name('product_attr')->where(['pid'=>1])->order('id asc')->select();
        $this->assign([
            'dep_Arr'       => $option,
            'departUserArr' => $departUserArr,
            'id'            => $uid,
            'schoolist'    => $schoolist,
            'gradelist'   => $gradelist
        ]);
    }
    public function arr2Tree($arr,$option='',$strk = ''){
        $str = '| - ';
        foreach($arr as $v){
            $option .= '<option value="'.$v['id'].'">'.$str.$strk.$v['name'].'</option>';
            if(!empty($v['children'])){
                $strk .= '- - ';
                $option = $this->arr2Tree($v['children'],$option,$strk);
                $strk = mb_substr($strk,4,mb_strlen($strk));
            }
        }
        return $option;
    }

    public function DeparArr($id){
        $arr= [];
        $departArr = Db::name('department')->where('pid',$id)->select();
        foreach ($departArr as $key => $value){
            $value['children'] = $this->DeparArr($value['id']);
            $arr[] = $value;
        }
        return $arr;
    }
    //Ajax 获取选择部门的职员信息
    public function departUser(){
        $depart_id = input('post.depart_id');
        // $roleModel = new RoleModel();
        // $departUserID = $roleModel->getUsersByDepartIdCansee($depart_id);
        // $where['id'] = array('in',$departUserID);

        $finance = new Finance();
        $departUserID = $finance->getAllRolesAndAllUsersByParentDepart($depart_id);
        $where['id'] = array('in',$departUserID['userArr']);
        //查询当前部门下的所有职员
        $departUserArr = Db::name('user')->field(['id','real_name'])->where($where)->select();
        return json(['code'=>1,'data'=>$departUserArr]);
    }
    /**
     * 数据统计
     * 客户统计/订单统计/业务统计
     * 黄
     */
    public function memberStatis ()
    {
        if (request()->isAjax()) {
			$param = input('param.');
            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;
            $where = [];
            if (!empty($param['name'])) {
                $where['name'] = ['like', '%' . $param['name'] . '%'];
            }
            if (!empty($param['phone'])) {
                $where['phone'] = ['like', '%' . $param['phone'] . '%'];
            }
            if (!empty($param['grade'])) {
                $where['grade'] = $param['grade'];
            }
            if (!empty($param['status'])) {
                $where['status'] = (int)$param['status'];
            }
            //时长
            if(!empty($param['timeLength'])){
                $time_step = $param['timeLength'] * 24 * 3600;
            }else{
                $time_step = 1 * 24 * 3600;
            }
            //时间判断
            if(!empty($param['time'])){
                $start_time = strtotime(explode(" - ",$param['time'])[0]." 00:00:00");
                $end_time = strtotime(explode(" - ",$param['time'])[1]." 23:59:59");
                $where['entry_time|second_time']=array('between',array($start_time,$end_time));
            }else{
                $start_time = strtotime(date("Y-m-d")." 00:00:00");
                $end_time = strtotime(date("Y-m-d")."23:59:59");
                $where['entry_time|second_time']=array('between',array($start_time,$end_time));
            }
            //depart_id user_id
            if (!empty($param['depart_id'])) {
                // $roleModel = new RoleModel();
                // $canSeeId = $roleModel->getUsersByDepartId2Cansee($param['depart_id']);
                $finance = new Finance();
                $departUserID = $finance->getAllRolesAndAllUsersByParentDepart($param['depart_id']);
        				// if(empty($canSeeId)){
        				// 	// $this->error('该部门缺少职位,请联系管理员添加');
        				// 	return ['total'=>0,'rows'=>'','code'=>'-1','msg'=>'该部门缺少职位,请联系管理员添加'];
        				// }else{
        				// 	$departUserID = explode(',',$canSeeId['can_see']);
        				// }
                $where['create_uid'] = array('in',$departUserID['userArr']);
            }else{
				        $canSee = session('can_see');
				        $creaArr = [];
        				foreach($canSee as $val){
          					if(empty($val)){
          						$creaArr = Db::name('user')->column('id');
          					}else{
          						array_push($creaArr,$val['create_uid']);
          					}
        				}
				        $where['create_uid'] = array('in',$creaArr);
			      }
            if(!empty($param['user_id'])){
                $where['create_uid'] = array('in',$param['user_id']);
            }
            $where['delete_status'] = array('neq',2);
            $member = new MemberModel();
			      $selectResult = $member->getMemberByWhere($where, $offset, $limit);
            foreach($selectResult as $key=>&$vo){
                $vo['name'] = '<a href="'.url('member/memberInfo',['m_id'=>$vo['id']]).'">'.$vo['name'].'</a>';
                $vo['status'] = Db::name('member_attr')->where(['id'=>$vo['status']])->value('name');//状态
                $vo['grade'] = Db::name('member_attr')->where(['id'=>$vo['grade']])->value('name');//等级
            }
            $total = $member->getAllMember($where);  // 总数据
            $return['total'] = $total;
            $return['rows'] = $selectResult;
            return json($return);
        }
        //客户等级
        $gradeArr = Db::name('member_attr')->where(['pid'=>1])->select();
        //客户状态
        $statusArr = Db::name('member_attr')->where(['pid'=>5])->select();
        $this->assign([
            'gradeArr' => $gradeArr,
            'statusArr' => $statusArr
        ]);
        return $this->fetch();
    }
    // 导出全部
    /**
  	 * [导出全部数据]
  	 */
  	public function memberAllExpotr(){
  		$res = DB::name('member')->where(['delete_status'=>1])->select();
  		// $res = $this->translateFince($res);
  		$data = [];
  		foreach($res as $k => $v){
  			$data[$k]['id'] = $v['id'];
  			$data[$k]['name'] = $v['name'];
  			$data[$k]['phone'] = $v['phone'];
  			$data[$k]['adress'] = $v['adress'];
  			$data[$k]['qq'] = $v['qq'];
  			$data[$k]['weixin'] = $v['weixin'];
  			$data[$k]['grade'] = Db::name('member_attr')->where(['id'=>$v['grade']])->value('name');
  			$data[$k]['status'] = Db::name('member_attr')->where(['id'=>$v['status']])->value('name');
  			$data[$k]['first_relation'] = Db::name('user')->where(['id'=>$v['first_relation']])->value('real_name');
  			$data[$k]['entry_time'] = date("Y-m-d",$v['entry_time']);
  			$data[$k]['remarks'] = $v['remarks'];
  		}
  		$savePath = explode('../application/',APP_PATH)[0].'upload/excel/';
  		$title = [
  			'会员ID',
  			'姓名',
  			'联系电话',
  			'地址',
  			'QQ',
  			'微信',
  			'等级',
  			'状态',
  			'录入人',
  			'录入时间',
  			'备注'
  		];
      $export = new Finance();
  		$export->exportExcel($title,$data, 'download', $savePath, false);
  	}
    // 客户统计
    public function ajaxStatis (){
        if (request()->isAjax()) {
            $param = input('param.');
            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;
            $where = [];
            if (!empty($param['name'])) {
                $where['name'] = ['like', '%' . $param['name'] . '%'];
            }
            if (!empty($param['phone'])) {
                $where['phone'] = ['like', '%' . $param['phone'] . '%'];
            }

			if(!empty($param['timeLength'])){
                $time_step = $param['timeLength'] * 24 * 3600;
            }else{
                $time_step = 1 * 24 * 3600;
            }

            if (!empty($param['grade'])) {
                $where['grade'] = $param['grade'];
            }
            if (!empty($param['status'])) {
                $where['status'] = (int)$param['status'];
            }
            //depart_id user_id
            if (!empty($param['depart_id'])) {
                // $roleModel = new RoleModel();
                // $departUserID = $roleModel->getUsersByDepartIdCansee($param['depart_id']);
                $finance = new Finance();
                $departUserID = $finance->getAllRolesAndAllUsersByParentDepart($param['depart_id']);
                $where['create_uid'] = array('in',$departUserID['userArr']);
            }else{
				$canSee = session('can_see');
				$creaArr = [];
				foreach($canSee as $val){
					if(empty($val)){
						$creaArr = Db::name('user')->column('id');
					}else{
						array_push($creaArr,$val['create_uid']);
					}
				}
				$where['create_uid'] = array('in',$creaArr);
			}
            if(!empty($param['user_id'])){
                $where['create_uid'] = array('in',$param['user_id']);
            }
            $where['delete_status'] = array('neq',2);
            $member = new MemberModel();
			$selectResult = $member->getMemberByWhere($where, $offset, $limit);
            //时间
            if(!empty($param['timeInterval'])){
                $start_time = strtotime(explode(" - ",$param['timeInterval'])[0]." 23:59:59");
                $end_time = strtotime(explode(" - ",$param['timeInterval'])[1]." 23:59:59");
            }else{
                $end_time = strtotime(date('Ymd',time())."235959");
                $start_time = strtotime(date('Ymd',strtotime("-1 months"))."235959");
            }
			$pointNum = ceil(($end_time - $start_time) / $time_step);
			$Xdata = [];
			$redData = [];
            $blueData = [];
            $greenData = [];
            $kingData = [];
			for($i=0;$i<=$pointNum;$i++){
				$a = $start_time + $i * $time_step;//第i个时间
				array_push($Xdata,date("Y-m-d",($a)));
				if($i==0){
					$where['time'] = array('between',[$start_time,$start_time - $time_step]);
				}else{
					$where['time'] = array('between',[$a,$a + $time_step]);
				}
				$redData['data'][$i] = 0;
				$blueData['data'][$i] = 0;
				$greenData['data'][$i] = 0;
				$kingData['data'][$i] = 0;
				foreach($selectResult as $key=>$vo){
					if($i==0){
						if(strtotime($vo['time']) >= $start_time - $time_step && strtotime($vo['time']) < $start_time && $vo['status']==6){
							$redData['data'][$i] += 1;
						}
						if(strtotime($vo['time']) >= $start_time - $time_step && strtotime($vo['time']) < $start_time && $vo['status']==7){
							$blueData['data'][$i] += 1;
						}
						if(strtotime($vo['time']) >= $start_time - $time_step && strtotime($vo['time']) < $start_time && $vo['status']==8){
							$greenData['data'][$i] += 1;
						}
						if(strtotime($vo['time']) >= $start_time - $time_step && strtotime($vo['time']) < $start_time && $vo['status']==9){
							$kingData['data'][$i] +=  1;
						}
					}else{
						if(strtotime($vo['time']) >= $a - $time_step && strtotime($vo['time']) < $a && $vo['status']==6){
							$redData['data'][$i] += 1;
						}
						if(strtotime($vo['time']) >= $a - $time_step && strtotime($vo['time']) < $a && $vo['status']==7){
							$blueData['data'][$i] += 1;
						}
						if(strtotime($vo['time']) >= $a - $time_step && strtotime($vo['time']) < $a && $vo['status']==8){
							$greenData['data'][$i] += 1;
						}
						if(strtotime($vo['time']) >= $a - $time_step && strtotime($vo['time']) < $a && $vo['status']==9){
							$kingData['data'][$i] += 1;
						}
					}
				}
			}
			$redData['name'] = "跟踪";
			$blueData['name'] = "成交";
			$greenData['name'] = "释放";
			$kingData['name'] = "公共";
            return ['code'=>1,'Xdata'=>$Xdata,'redData'=>$redData,'blueData'=>$blueData,'greenData'=>$greenData,'kingData'=>$kingData,'Msg'=>'图表加载成功'];
        }
    }
    /**
     * 业务统计
     * 选择时间内 录入订单
     * 选择时间内 订单款项
     */
    public function businStatis ()
    {
		$uid = session('id');
		if(request()->isAjax()){
			$param = input('param.');
			$limit = $param['pageSize'];
			$offset = ($param['pageNumber'] - 1) * $limit;
			$where = [];
			//客户姓名
			if(!empty($param['stu_name'])){
				$where['stu_name'] = array('like','%'.$param['stu_name'].'%');
			}
			//时间
			if(!empty($param['c_time'])){
				$start_time = strtotime(explode(" - ",$param['c_time'])[0]." 235959");
				$end_time = strtotime(explode(" - ",$param['c_time'])[1]." 235959");
				$where['c_time'] = array('between',[$start_time,$end_time]);
			}else{
				$start_time = strtotime(date('Ymd',time())." 000000");
                $end_time = strtotime(date('Ymd',time())." 235959");
				$where['c_time'] = array('between',[$start_time,$end_time]);
			}
			$where['delete_status'] = array('neq',1);

      // 2017-7-16 (新增查询条件[order_sn school_id major_id grade batch sort])
      if(!empty($param['order_sn'])){
          $where['order_sn'] = array('like','%'.$param['order_sn'].'%');
      }
      if(!empty($param['school_id'])){
          $where['school_id'] = array('eq',$param['school_id']);
      }
      if(!empty($param['major_id'])){
          $where['school_id'] = array('eq',$param['school_id']);
          $where['major_id'] = array('eq',$param['major_id']);
      }
      if(!empty($param['grade'])){
          $where['grade'] = array('eq',$param['grade']);
      }
      if(!empty($param['batch'])){
          $where['batch'] = array('like','%'.$param['batch'].'%');
      }
      // $where['sort'] = $param['sortName'].' '.$param['sortOrder'];
      $sort = $param['sortName'].' '.$param['sortOrder'];
			$order = new OrderModel();

			//所属咨询师(成交人员)
			if(!empty($param['depart_id']) || !empty($param['user_id'])){
				//depart_id user_id
        if (!empty($param['depart_id'])) {
            $finance = new Finance();
            $departUserID = $finance->getAllRolesAndAllUsersByParentDepart($param['depart_id']);
            $where['consultant'] = array('in',$departUserID['userArr']);
        }
        if(!empty($param['user_id'])){
            $where['consultant'] = array('in',$param['user_id']);
        }
			}else{
				$canSee = session('can_see');
				$creaArr = [];
				foreach($canSee as $val){
					if(empty($val)){
						$creaArr = Db::name('user')->column('id');
					}else{
						array_push($creaArr,$val['create_uid']);
					}
				}
				$where['consultant'] = array('in',$creaArr);
			}
      $where['status'] = ['neq',4];
			$orderArr = $order->getMyOrder($where, $offset, $limit,$sort);
			foreach ($orderArr as $key=>&$value){
        $value['stu_name'] = '<a href="'.url('member/memberInfo',['m_id'=>$value['m_id']]).'">'.$value['stu_name'].'</a>';
				$u_id = $value['m_id'];
				// $payment_method  = $value['payment_method'];//缴费方式
				$consultant = $value['consultant'];
        //批次
        // $batch = Db::name('student')->where(['m_id'=>$u_id])->value('batch');
        //学习形式
        $acquisition_mode = Db::name('product_attr')->where(['id'=>$value['acquisition_mode']])->value('name');
				//层次
				$gradeN = Db::name('product_attr')->where(['id'=>$value['grade']])->value('name');
        //学制
        $system = Db::name('product_attr')->where(['id'=>$value['system']])->value('name');
        //经办人
        $agent = Db::name('user')->where(['id'=>$value['agent']])->value('real_name');
        //所属咨询师
        $consultantN = Db::name('user')->where('id',$consultant)->find();
        //所属部门
        $depart_id = Db::name('role')->where(['id'=>$consultantN['role_id']])->value('department_id');
        $depa_name = Db::name('department')->where(['id'=>$depart_id])->value('name');
        //当前订单的所属客户信息
        $memberArr = Db::name('member')->where(['id'=>$u_id])->find();
        //生源途径
        $source_origin = Db::name('member_attr')->where(['id'=>$memberArr['source_origin']])->value('name');

				$mem_phone = Db::name('member')->where(['id'=>$u_id])->value('phone');//查询当前订单的客户信息
				// $payment_methodN = Db::name('product_attr')->where(['id'=>$payment_method])->value('name');
        // $value['batch'] = $batch;
        $value['acquisition_mode'] = $acquisition_mode;
        $value['gradeN'] = $gradeN;
        $value['system'] = $system;
        $value['agent'] = $agent;
        $value['depa_name'] = $depa_name;
        //隐藏
        $value['sex'] = $memberArr['sex'];
        $value['nation'] = $memberArr['nation'];
        $value['industry'] = $memberArr['industry'];
        $value['region'] = $memberArr['region'];
        $value['native_place'] = $memberArr['native_place'];
        $value['id_card'] = $memberArr['id_card'];
        $value['education'] = $memberArr['education'];
        $value['graduate_school'] = $memberArr['graduate_school'];
        $value['certificate_number'] = $memberArr['certificate_number'];
        $value['graduation_time'] = $memberArr['graduation_time'];
        $value['phone'] = $memberArr['phone'];
        $value['qq'] = $memberArr['qq'];
        $value['weixin'] = $memberArr['weixin'];
        $value['source_origin'] = $source_origin;

				// $value['payment_methodN'] = $payment_methodN;
				$value['consultantN'] = $consultantN['real_name'];
				$value['c_time'] = date('Y-m-d',$value['c_time']);
        //新增
        $value['order_sn'] = $value['order_sn'];
			}
      // print_r($orderArr);die;
			$return['total'] = $order->getAllOrder($where);  // 总数据
      // print_r($return['total']);die;
			$return['rows'] = $orderArr;
			$return['sum'] = 0;
			foreach($return['rows'] as $val){
				//增加总金额
				$return['sum'] += $val['total_amount'];
			}
			return json($return);
		}
		return $this->fetch();
    }

    // 导出全部
    /**
  	 * [导出全部数据]
  	 */
  	public function businAllExpotr(){
  		$res = DB::name('order')->where(['delete_status'=>0,'status'=>['neq',4]])->select();
  		// $res = $this->translateFince($res);
  		$data = [];
  		foreach($res as $k => $v){
        $memberArr = Db::name('member')->where(['id'=>$v['m_id']])->find();
  			$data[$k]['id'] = $v['id'];  //订单ID
  			$data[$k]['order_sn'] = $v['order_sn'];//单号
  			$data[$k]['c_time'] = date('Y-m-d',$v['c_time']);//创建时间
  			$data[$k]['batch'] = $v['batch'];//批次
  			$data[$k]['name'] = $memberArr['name'];//姓名
  			$data[$k]['school'] = $v['school'];//学院
  			$data[$k]['grade'] = Db::name('product_attr')->where(['id'=>$v['grade']])->value('name');//层次
        $data[$k]['major'] = $v['major'];//专业
  			$data[$k]['acquisition_mode'] = Db::name('product_attr')->where(['id'=>$v['acquisition_mode']])->value('name');// 教育模式
  			$data[$k]['system'] = Db::name('product_attr')->where(['id'=>$v['system']])->value('name');//学制
  			$data[$k]['tuition'] = $v['tuition'];//学费
        $data[$k]['registration'] = $v['registration'];//报名费
        $data[$k]['material_cost'] = $v['material_cost'];//资料费
        $data[$k]['photo_collection'] = $v['photo_collection'];//照片采集费
  			$data[$k]['agent'] = Db::name('user')->where(['id'=>$v['agent']])->value('real_name');//经办人
        //所属咨询师
        $consultantN = Db::name('user')->where('id',$v['consultant'])->find();
        //所属部门
        $depart_id = Db::name('role')->where(['id'=>$consultantN['role_id']])->value('department_id');
        $depa_name = Db::name('department')->where(['id'=>$depart_id])->value('name');
        $data[$k]['consultant'] = $consultantN['real_name'];//所属咨询师
        $data[$k]['department'] = $depa_name;//所属部门
        $data[$k]['sex'] = $memberArr['sex'];//性别
        $data[$k]['nation'] = $memberArr['nation'];//民族
        $data[$k]['industry'] = $memberArr['industry'];//职业
        $data[$k]['region'] = $memberArr['region'];//地区
        $data[$k]['native_place'] = $memberArr['native_place'];//籍贯
        $data[$k]['id_card'] = $memberArr['id_card'];//身份证号码
        $data[$k]['education'] = $memberArr['education'];//学历
        $data[$k]['graduate_school'] = $memberArr['graduate_school'];//毕业院校
        $data[$k]['certificate_number'] = $memberArr['certificate_number'];//证书编号
        $data[$k]['graduation_time'] = $memberArr['graduation_time'];//毕业时间
        $data[$k]['phone'] = $memberArr['phone'];//联系电话
        $data[$k]['qq'] = $memberArr['qq'];//QQ
        $data[$k]['weixin'] = $memberArr['weixin'];//微信
        $data[$k]['source_origin'] = Db::name('member_attr')->where(['id'=>$memberArr['source_origin']])->value('name');;//生源途径
  		}
  		$savePath = explode('../application/',APP_PATH)[0].'upload/excel/';
  		$title = [
  			'订单ID',
  			'订单单号',
  			'创建时间',
  			'批次',
  			'姓名',
  			'报考学院',
  			'层次',
  			'专业',
  			'学习形式',
  			'学制',
  			'学费',
        '报名费',
        '资料费',
        '照片采集费',
        '经办人',
        '所属咨询师',
        '所属部门',
        '性别',
        '民族',
        '职业',
        '地区',
        '籍贯',
        '身份证号码',
        '学历',
        '毕业院校',
        '证书编号',
        '毕业时间',
        '联系电话',
        'QQ号',
        '微信号',
        '生源途径',
  		];
      $export = new Finance();
  		$export->exportExcel($title,$data, 'download', $savePath, false);
  	}
    //Ajax获取不同学校的不同专业
    public function ajaxMajor(){
        $school_id = input('school_id');
        if(!empty($school_id)){
            $majorList = Db::name('school_specialty')->where(['pid'=>$school_id])->order('id asc')->select();
        }else{
            $majorList = [];
        }
        return json(['code'=>1,'data'=>$majorList]);
    }
	  //业务业绩图表
    public function ajaxBusinStatis ()
    {
        if(request()->isAjax()){
            $uid = session('id');
            $param = input('param.');
            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;
            $where = [];
            //客户姓名
            if(!empty($param['stu_name'])){
                $where['stu_name'] = array('like','%'.$param['stu_name'].'%');
            }
            //时间
            if(!empty($param['timeInterval'])){
                $start_time = strtotime(explode(" - ",$param['timeInterval'])[0]." 235959");
                $end_time = strtotime(explode(" - ",$param['timeInterval'])[1]." 235959");
                $where['c_time'] = array('between',[$start_time,$end_time]);
            }else{
                $start_time = strtotime(date('Ymd',strtotime("-1 months"))." 235959");
                $end_time = strtotime(date('Ymd',time())." 235959");
                $where['c_time'] = array('between',[$start_time,$end_time]);
            }
            $where['delete_status'] = array('neq',1);
            // 时间(步长)
            if(!empty($param['timeLength'])){
                $timeLength = $param['timeLength'] * 24 * 3600;
            }else{
                $timeLength = 1 * 24 * 3600;
            }

            $order = new OrderModel();

            //所属咨询师(成交人员)
            if(!empty($param['depart_id']) || !empty($param['user_id'])){
                //depart_id user_id
                if (!empty($param['depart_id'])) {
                    // $roleModel = new RoleModel();
                    // $departUserID = $roleModel->getUsersByDepartIdCansee($param['depart_id']);
                    $finance = new Finance();
                    $departUserID = $finance->getAllRolesAndAllUsersByParentDepart($param['depart_id']);
                    $where['consultant'] = array('in',$departUserID['userArr']);
                }
                if(!empty($param['user_id'])){
                    $where['consultant'] = array('in',$param['user_id']);
                }
            }else{
				$canSee = session('can_see');
				$creaArr = [];
				foreach($canSee as $val){
					if(empty($val)){
						$creaArr = Db::name('user')->column('id');
					}else{
						array_push($creaArr,$val['create_uid']);
					}
				}
				$where['consultant'] = array('in',$creaArr);
			}

            $orderArr = $order->getMyOrder($where, $offset, $limit);

            $Xdata = [];
            $kingData = [];
            $timeSutp = ceil($end_time - $start_time) / $timeLength;
            for ($i = 0; $i <= $timeSutp; $i++){
                $Ktime = $start_time + $i * $timeLength;
                array_push($Xdata,date('Y-m-d',$Ktime));
                $kingData['data'][$i] = 0;
                foreach ($orderArr as $key => $val){
                    if($i == 0){
                        if($val['c_time'] >= ($start_time - $timeLength) && $val['c_time'] < $start_time){
                            $kingData['data'][$i] += $val['total_amount'];
                        }
                    }else{
                        if($val['c_time'] >= ($Ktime - $timeLength) && $val['c_time'] < $Ktime){
                            $kingData['data'][$i] += $val['total_amount'];
                        }
                    }
                }
            }
            $kingData['name'] = '总业绩';
            return  ['code'=>1,'Xdata'=>$Xdata,'kingData' => $kingData,'Msg'=>'图表加载成功'];
        }
    }
}
